package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.RoleDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleLevelVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.role.RoleCreateEvent;
import com.wan37.logic.service.RoleService;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RoleLevelUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBase;
import com.wan37.protobuf.message.client2server.game.GameRole;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : luoyong
 * @date : 2020-07-24 10:49
 **/
public class CGameCreatRoleHandler extends MsgHandler {
    private RoleService roleService;
    private RouteUtils routeUtils;
    private RoleServiceCache roleServiceCache;
    private UserServiceCache userServiceCache;
    private RoleLevelUtil roleLevelUtil;
    private AsynDBManager asynDBManager;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameCreatRoleHandler(MessageVO messageVO) {
        super(messageVO);
        this.roleService = SpringContextHolder.getBean("roleService");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.roleLevelUtil = SpringContextHolder.getBean("roleLevelUtil");
        this.asynDBManager = SpringContextHolder.getBean("asynDBManager");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameRole.CGameCreateRoleMsg cGameCreateRoleMsg = (GameRole.CGameCreateRoleMsg) messageVO.getMessage();
        String profession = cGameCreateRoleMsg.getProfession();
        List<RoleDO> roleDOList = roleService.getRolesByUserId(messageVO.getUserId());

        if (roleDOList != null && roleDOList.size() > 3) {
            routeUtils.sendResponse(Common.GAME_CREATE_ROLE_ERROR,
                    "角色已经达到上限！", messageVO.getNetId(), messageVO.getUserId());
            return;
        }
        RoleVO roleVO = new RoleVO();
        if (roleDOList == null) {
            roleVO.setAllRoleId("1" + messageVO.getUserId() + profession + 0);

        } else {
            roleVO.setAllRoleId("1" + messageVO.getUserId() + profession + roleDOList.size());
        }
        roleVO.setLevel(1);
        roleVO.setNumberMap(new ConcurrentHashMap<>());
        RoleLevelVO roleLevelVO = roleLevelUtil.userRoleLevelMap.get("1");
        roleVO.getNumberMap().put(CommonBase.ATN, roleLevelVO.getATN());
        roleVO.getNumberMap().put(CommonBase.INT, roleLevelVO.getINT());
        roleVO.getNumberMap().put(CommonBase.DEF, roleLevelVO.getDEF());
        roleVO.getNumberMap().put(CommonBase.RES, roleLevelVO.getRES());
        roleVO.getNumberMap().put(CommonBase.MP, roleLevelVO.getMP());
        roleVO.getNumberMap().put(CommonBase.HP, roleLevelVO.getHP());
        roleVO.setStatus(AllRoleLabel.STATUS_LIFE);
        roleVO.setLabel(AllRoleLabel.ROLE);
        roleVO.setProfession(profession);
        roleVO.setUserId(messageVO.getUserId());
        roleVO.setName(cGameCreateRoleMsg.getRoleName());
        RoleServiceCache.Role role = roleServiceCache.new Role();
        role.setRoleVO(roleVO);
        roleServiceCache.getRoleServiceMap().put(roleVO.getAllRoleId(), role);
        if (userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getRoleIdList() == null) {
            userServiceCache.getUserServiceMap().get(messageVO.getUserId()).setRoleIdList(new ArrayList<>());
        }
        userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getRoleIdList().add(roleVO.getAllRoleId());
        asynDBManager.distributeWork(messageVO.getUserId(), () -> roleService.save(new RoleDO(roleVO)));
        routeUtils.sendResponse(Common.GAME_CREATE_ROLE, "创建成功", messageVO.getNetId(), messageVO.getUserId());
        /**
         * 触发创建新角色事件
         */
        EventDispatcher.getInstance().fireEvent(new RoleCreateEvent
                (EventType.ROLE_CREATE, messageVO.getUserId(), roleVO.getAllRoleId()));
    }
}
